<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录" />
    <!-- /导航栏 -->

    <!-- 登录表单 -->
    <van-form @submit="onSubmit" >
      <!-- 手机号 -->
      <van-field
       :rules="userFormRules.mobile"
        name="用户名"
        placeholder="请输入手机号"
        v-model="user.mobile"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
    <!-- 验证码 -->
      <van-field
       :rules="userFormRules.code"
      v-model="user.code"
        type="password"
        name="验证码"
        placeholder="请输入验证码"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
       <van-count-down
       v-if='isCountDownShow'
       slot="button"
       :time="1000*5"
       format="ss s"
       @finish="isCountDownShow = false"
       />

          <van-button
          class="send-sms-btn"
          round
          size="small"
          type="default"
          v-else
          slot="button"
          @click="onSendSmsCode"
          >发送验证码</van-button>

      </van-field>
      <div class="login-btn-wrap">
        <van-button
        class="login-btn"
        block type="info"
        native-type="submit"
        @click="onLogin"
        >
          登录
        </van-button>
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>

<script>
import { login, getCode } from '@/api/user.js'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      isCountDownShow: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {

    // 登录请求
    async onSubmit () {
      const user = this.user
      try {
        const res = await login(user)
        console.log(res)
        if (res.data.message !== 'OK') return this.$toast.fail('登录失败')
        this.$store.commit('setUserToken', res.data.data)
        this.$toast.success('登录成功')
        this.$router.push('/')
      } catch (err) {
        console.log(err)
        this.$toast.fail('登录失败')
        // if (err.response.status === 400) {
        //   console.log('登录失败', err)
        // }
      }
    },

    // 登录等待界面
    async onLogin () {
      // 开始转圈圈
      this.$toast.loading({
        duration: 0, // 持续时间，0表示持续展示不停止
        forbidClick: true, // 是否禁止背景点击
        message: '登录中...' // 提示消息
      })

      try {
        const res = await login(this.user)
        console.log('登录成功', res)
        // 提示 success 或者 fail 的时候，会先把其它的 toast 先清除
        this.$toast.success('登录成功')
      } catch (err) {
        console.log('登录失败', err)
        this.$toast.fail('登录失败，手机号或验证码错误')
      }
    },
    // 发送验证码
    async onSendSmsCode () {
      this.isCountDownShow = true
      try {
        const res = await getCode(this.user.mobile)
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 200px;
    height: 40px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
